package de.szut.sudoku.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.AbstractButton;

/**
 * Container that manages the available languages
 * 
 * @author dqi12huisinga
 * 
 */
public class LanguageContainer {
	public static final String defaultLanguage = "English";
	public static final String[] languages = { "English", "German", "Italian",
			"French", "Polish", "Pirate" };

	private Map<String, AbstractButton> translatableComponents;
	private String currentLanguage;
	private File currentLanguageFile;
	private String filePathPrefix;

	public String getFilePathPrefix() {
		return filePathPrefix;
	}

	public void setFilePathPrefix(String filePathPrefix) {
		this.filePathPrefix = filePathPrefix;
	}

	public Map<String, AbstractButton> getTranslatableComponents() {
		return translatableComponents;
	}

	public void addTranslatableComponent(String key,
			AbstractButton translatableComponent) {
		translatableComponents.put(key, translatableComponent);
	}

	public String getCurrentLanguage() {
		return currentLanguage;
	}

	public void setCurrentLanguage(String currentLanguage) {
		this.currentLanguage = currentLanguage;
		currentLanguageFile = new File(filePathPrefix
				+ this.currentLanguage.toLowerCase() + ".ini");
	}

	/**
	 * Translates all registered components
	 * 
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void translate() throws FileNotFoundException, IOException {
		// reads the data of the current language
		Properties p = new Properties();
		p.load(new FileInputStream(currentLanguageFile));
		// translates all registered components
		for (Map.Entry<String, AbstractButton> currentEntry : translatableComponents
				.entrySet()) {
			currentEntry.getValue().setText(
					p.getProperty(currentEntry.getKey()));
		}
	}

	public LanguageContainer() {
		translatableComponents = new HashMap<String, AbstractButton>();
		filePathPrefix = "";
		setCurrentLanguage(defaultLanguage);
	}

}
